const app = getApp()
const utils = require('../../../utils/util')
Page({
  data: {
    active: 0, // 当前按条件排序 最新  按点赞数 按浏览量
    navHeight: app.globalData.navHeight,
    menuBotton: app.globalData.menuBotton ,
    menuRight: app.globalData.menuRight ,
    menuHeight: app.globalData.menuHeight,
    isIphoneX: app.globalData.isIphoneX,
    inputValue:'', // 输入框的值
    isLenovoSearch: 0, // 是否联想搜索 0  1
    historyRecord:[],//历史记录
    Lenovo:[], //联想数组
    searchResultList: '',  //结果
    model:false,
    isRefresh: false, // 是否刷新数据
    hasNoMore: false, // 是否有数据
    page: 1, // 当前第一页
    conditionList:[{ //条件 
        name:'最新',
        value: 1
      }, {
        name:'按点赞数',
        value: 2
      },  {
        name:'按浏览量',
        value: 3
    }],
    promptText:[
      {text: '方案名'}, {text: '风格'},{text: '系列'},{text: '小区名称'},
      {text: '空间'},{text: '设计师名'},{text: '专题'},{text: '家庭组成'},
      {text: '柜体色板'},{text: '门型色板'},{text: '柜体品类'}                      
    ]
  },
  //退回上个页面
  goBack(){
    wx.navigateBack()
  },
  //获取输入框的值
  bindKeyInput(e){  
    let inputValue = e.detail.value.replace(/\s+/g, '')
    let model = true
    if(inputValue.length===0){
      model=false
      this.setData({
        Lenovo:[]
      })
    }
    this.setData({
      inputValue,
      model
    })
    this.GetlenovoSearch(inputValue)
  },
  //清空输入框的值
  Empty(){
    console.log('empty-icon')
    let inputValue = ''
    this.setData({
      inputValue,
      isLenovoSearch: 0,
      model:false,
      Lenovo:[],
    })
  },
  //获取历史记录
  gethistoryRecord(){
    let historyRecord = wx.getStorageSync('historyRecord')

    this.setData({
      historyRecord : historyRecord.length ? historyRecord :[],
    })
  },
  //点击联想事件
  clickLenovo(e){
    let inputValue = e.target.dataset.inputvalue
    this.setData({
      inputValue,
      isLenovoSearch: 1
    })
    this.search(false)
  },
  //搜索事件
  search(flag){
    console.log('search-icon')
    let inputValue = this.data.inputValue
    if(inputValue.length === 0){
      return false
    }
    let historyRecord = wx.getStorageSync('historyRecord')
 
    const data = {
      value: inputValue,
      isLenovoSearch: flag ? 0 : 1
    }
    if(historyRecord.length){
      historyRecord.unshift(data)
    } else { 
      historyRecord = []
      historyRecord.unshift(data)
    }
    const record = this.uniqueArr(historyRecord).slice(0, 10)

    this.setData({
      model:false,
      Lenovo:[],
      active: 0,
      isLenovoSearch: flag ? 0 : 1
    })
    wx.setStorageSync('historyRecord', record)
    this.gethistoryRecord()
    
    // if(flag){
    //   this.getSearchList(inputValue) // 模糊搜索
    // } else {
    //   this.getSearchList(inputValue, 1) // 联想搜索
    // }
    this.data.page = 1
    this.getSearchList(this.data.page)
  },
  // 去重
  uniqueArr(arr){
    let hash = {};
    let newArr = arr.reduce((cur,next) => {
      hash[next.value] ? "" : hash[next.value] = true && cur.push(next);
      return cur;
    },[]) //设置cur默认类型为数组，并且初始值为空的数组
    return newArr;
  },
  //清除历史记录
  deleteHistoryRecord(){
    wx.removeStorageSync('historyRecord')
    this.gethistoryRecord()
  },
  //获取搜索联想
  GetlenovoSearch(keyWord){
    utils.$http(app.globalData.api.GetlenovoSearch, {
      keyWord,
    }, 'POST', true, 'JSON').then(res=>{
      if(res.data){
        let Lenovo = res.data
        this.setData({
          Lenovo
        })
      }
    })
  },
  // 全局搜索
  getSearchList(page, type = 'new') {
    const {isLenovoSearch, active, inputValue} = this.data
    utils.$http(app.globalData.api.GetglobalSearch, {
      keyWord: inputValue,
      isLenovoSearch,
      sort: active,
      page: page ? page : 1,
      rows: 10,
      
    }, 'POST', false, 'JSON').then(res=>{
      if(res.success === 1) {
        const  { records  = []} = res.data
        if (type === 'new') {
          this.setData({
            isRefresh: false,
            searchResultList: records,
            hasNoMore: false
          })
        } else {
          if(records.length === 0){
              this.setData({
                hasNoMore:true
              })
          } else {
            const list = this.data.searchResultList.concat(records)
            this.setData({
              searchResultList: list,
              isRefresh: false,
            })
          }
        }
      }
    })
  },
  //历史搜索
  historySearch(e) {
    console.log(e.currentTarget.dataset)
    const { item } = e.currentTarget.dataset
    let { value, isLenovoSearch } = item

    this.setData({
      inputValue:value,
      isLenovoSearch: isLenovoSearch,
      active: 0
    })

    const historyRecord = wx.getStorageSync('historyRecord')
    let newHistory = historyRecord.filter(item => item.value !== value)
    newHistory.unshift(item)
    wx.setStorageSync('historyRecord', newHistory)

    // this.gethistoryRecord()
    this.data.page = 1
    // this.getSearchList(value,isLenovoSearch)
    this.getSearchList(this.data.page)
  },
  //退出模态框
  exitModel(){
    this.setData({
      model:false,
      Lenovo:[],
      inputValue: ''
    })  
  },
  //改变条件
  setActive(e){
    let active = e.target.dataset.active
    // let inputValue = this.data.inputValue
    console.log(this.data)
    const { inputValue, isLenovoSearch } = this.data
    this.setData({
      active,  
    })
    // this.getSearchList(inputValue, isLenovoSearch ,active)
    this.data.page = 1
    this.getSearchList(this.data.page)
  },
  loadNewData() {
    this.data.isRefresh = true
    this.data.page = 1
    this.getSearchList(this.data.page)
  },
  loadMoreData() {
    this.data.page = this.data.page + 1
    this.getSearchList(this.data.page, 'more')
  },
  onLoad: function () {
    this.gethistoryRecord()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMoreData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})